﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Editor
{
    public partial class TileCreator : Form
    {
        private Image _tileSheetImage;
        private TileListWindow tileListWindow;
        private CompleteTile _workingTile;
        private CompleteTile _editingTile;
        private int _tileX, _tileY;
        private bool _editTile = false;

        public delegate void NulLDelegate();
        public delegate void ReturnTile(CompleteTile tile);
        public event ReturnTile CreatedNewTile;
        public event NulLDelegate UpdatedTile;
        public event NulLDelegate Hidden;
        
        public TileCreator(ref Image tiles)
        {
            _tileSheetImage = tiles;
            tileListWindow = new TileListWindow(ref _tileSheetImage);
            InitializeComponent();
            tileListWindow.TileSelected+=new TileListWindow.PassBitmap(RecieveTile);
        }

        private void TileCreator_Load(object sender, EventArgs e)
        {
        }

        private void RecieveTile(Bitmap tileImage, int X, int Y)
        {
            _tileX = X;
            _tileY = Y;
            tileImageButton.Image = tileImage;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
        }
        public void EditTile(iTile tile)
        {
            _editingTile = (CompleteTile)tile;
            tileImageButton.Image = _editingTile.tileImage;
            _editTile = true;
            this.Show();
        }


        private void tileImageButton_click(object sender, EventArgs e)
        {
            tileListWindow.Show();
        }

        private void acceptButton_Click(object sender, EventArgs e)
        {
            if (_editTile)
            {
                EditTileData();
                _editTile = false;
                this.Hide();
                UpdatedTile();
                Hidden();
                return;
            }
            CreateWorkingTileData();
            CreatedNewTile(_workingTile);
            this.Hide();
            Hidden();
        }

        private void EditTileData()
        {
            _editingTile.collidable = collisionCheckBox.Checked;
            _editingTile.key = keyCheckBox.Checked;
            _editingTile.monster = monsterCheckBox.Checked;
            _editingTile.start = startCheckBox.Checked;
            _editingTile.finish = finishCheckBox.Checked;
            _editingTile.treasure = treasureCheckBox.Checked;
            _editingTile.name = nameTextBox.Text;
            _editingTile.tileImage = (Bitmap)tileImageButton.Image;
            _editingTile.imageXLocation = _tileX;
            _editingTile.imageYLocation = _tileY;
        }

        private void CreateWorkingTileData()
        {
            _workingTile = new CompleteTile(this.nameTextBox.Text, collisionCheckBox.Checked, (Bitmap)tileImageButton.Image);
            _workingTile.imageXLocation = _tileX;
            _workingTile.key = keyCheckBox.Checked;
            _workingTile.monster = monsterCheckBox.Checked;
            _workingTile.start = startCheckBox.Checked;
            _workingTile.finish = finishCheckBox.Checked;
            _workingTile.treasure = treasureCheckBox.Checked;
            _workingTile.imageYLocation = _tileY;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.tileListWindow.Hide();
            this.Hide();
            Hidden();
        }
    }
}
